package com.example.blackbox.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.blackbox.Activity.PageActivity;
import com.example.blackbox.Adapter.NewsAdapter;
import com.example.blackbox.Adapter.OtherNewsAdapter;
import com.example.blackbox.Data.JsonTool;
import com.example.blackbox.Data.News;
import com.example.blackbox.Net.NetTool;
import com.example.blackbox.R;

import java.io.Serializable;
import java.util.ArrayList;

public class IndexFragment extends Fragment {

    ListView newsLsit;
    NewsAdapter newsAdapter;
    OtherNewsAdapter otherNewsAdapter;
    Handler handler,jsonHandler;
    String data;
    ArrayList<News> news;
    ArrayList<News> otherNews=new ArrayList<>();
    LinearLayout all,pcgame,ow,cbpk;
    View allLine,pcgameLine,owLine,cbpkLine;
    LinearLayout menu;
    private float moveX;
    private float moveY;
    private float pressX;
    private float pressY;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_index,null);

        return view;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initUI();
        menu=getActivity().findViewById(R.id.index_ll_menu);
        registerForContextMenu(menu);
        //设置json数据handler
        jsonHandler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                newsAdapter=new NewsAdapter(news,getContext());
                newsLsit.setAdapter(newsAdapter);
            }
        };

        //设置网络handler
        handler = new Handler() {  //如果服务器返回值为“ok”证明用户名密码正确并跳转登录后界面否则给出相应的提示信息
            @Override
            public void handleMessage(Message msg) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        news = JsonTool.getNews(data);
                        Message m=jsonHandler.obtainMessage();
                        jsonHandler.sendMessage(m);
                    }
                }).start();
            }};


        //请求文章
        new Thread(new Runnable() {
            @Override
            public void run() {
                data = NetTool.getJsonString("http://10.0.2.2:8080/androids/json/news.json");
                Message m=handler.obtainMessage();
                handler.sendMessage(m);
            }
        }).start();

        newsLsit.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position!=0){
                    Intent intent=new Intent(getActivity(), PageActivity.class);
                    Bundle bundle=new Bundle();
                    bundle.putSerializable("news", news.get(position));
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            }
        });


        all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                newsLsit.setAdapter(newsAdapter);
                allLine.setVisibility(View.VISIBLE);
                pcgameLine.setVisibility(View.GONE);
                owLine.setVisibility(View.GONE);
                cbpkLine.setVisibility(View.GONE);

            }
        });

        pcgame.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                otherNews.clear();
                for (int i = 0; i < news.size(); i++) {
                    if ("PC游戏".equals(news.get(i).getNewsTag())){
                        otherNews.add(news.get(i));
                    }
                }

                otherNewsAdapter=new OtherNewsAdapter(otherNews);
                newsLsit.setAdapter(otherNewsAdapter);
                allLine.setVisibility(View.GONE);
                pcgameLine.setVisibility(View.VISIBLE);
                owLine.setVisibility(View.GONE);
                cbpkLine.setVisibility(View.GONE);
            }
        });

        ow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                otherNews.clear();
                for (int i = 0; i < news.size(); i++) {
                    if ("守望先锋".equals(news.get(i).getNewsTag())){
                        otherNews.add(news.get(i));
                    }
                }

                otherNewsAdapter=new OtherNewsAdapter(otherNews);
                newsLsit.setAdapter(otherNewsAdapter);
                allLine.setVisibility(View.GONE);
                pcgameLine.setVisibility(View.GONE);
                owLine.setVisibility(View.VISIBLE);
                cbpkLine.setVisibility(View.GONE);
            }
        });

        cbpk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                otherNews.clear();
                for (int i = 0; i < news.size(); i++) {
                    if ("赛博朋克".equals(news.get(i).getNewsTag())){
                        otherNews.add(news.get(i));
                    }
                }

                otherNewsAdapter=new OtherNewsAdapter(otherNews);
                newsLsit.setAdapter(otherNewsAdapter);
                allLine.setVisibility(View.GONE);
                pcgameLine.setVisibility(View.GONE);
                owLine.setVisibility(View.GONE);
                cbpkLine.setVisibility(View.VISIBLE);
            }
        });



    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        MenuInflater inflater = getActivity().getMenuInflater();
        inflater.inflate(R.menu.item_menu, menu);
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch(item.getItemId()){
            case R.id.it1:
                newsLsit.setAdapter(newsAdapter);
                allLine.setVisibility(View.VISIBLE);
                pcgameLine.setVisibility(View.GONE);
                owLine.setVisibility(View.GONE);
                cbpkLine.setVisibility(View.GONE);
                break;

            case R.id.it2:
                otherNews.clear();
                for (int i = 0; i < news.size(); i++) {
                    if ("PC游戏".equals(news.get(i).getNewsTag())){
                        otherNews.add(news.get(i));
                    }
                }

                otherNewsAdapter=new OtherNewsAdapter(otherNews);
                newsLsit.setAdapter(otherNewsAdapter);
                allLine.setVisibility(View.GONE);
                pcgameLine.setVisibility(View.VISIBLE);
                owLine.setVisibility(View.GONE);
                cbpkLine.setVisibility(View.GONE);
                break;

            case R.id.it3:
                otherNews.clear();
                for (int i = 0; i < news.size(); i++) {
                    if ("守望先锋".equals(news.get(i).getNewsTag())){
                        otherNews.add(news.get(i));
                    }
                }

                otherNewsAdapter=new OtherNewsAdapter(otherNews);
                newsLsit.setAdapter(otherNewsAdapter);
                allLine.setVisibility(View.GONE);
                pcgameLine.setVisibility(View.GONE);
                owLine.setVisibility(View.VISIBLE);
                cbpkLine.setVisibility(View.GONE);
                break;

            case R.id.it4:
                otherNews.clear();
                for (int i = 0; i < news.size(); i++) {
                    if ("赛博朋克".equals(news.get(i).getNewsTag())){
                        otherNews.add(news.get(i));
                    }
                }

                otherNewsAdapter=new OtherNewsAdapter(otherNews);
                newsLsit.setAdapter(otherNewsAdapter);
                allLine.setVisibility(View.GONE);
                pcgameLine.setVisibility(View.GONE);
                owLine.setVisibility(View.GONE);
                cbpkLine.setVisibility(View.VISIBLE);
                break;

                default:
                    break;
        }
        return true;
    }

    public void initUI(){
        newsLsit=getActivity().findViewById(R.id.ls);
        all=getActivity().findViewById(R.id.index_ll_all);
        pcgame=getActivity().findViewById(R.id.index_ll_pagame);
        ow=getActivity().findViewById(R.id.index_ll_ow);
        cbpk=getActivity().findViewById(R.id.index_ll_cbpk);

        allLine=getActivity().findViewById(R.id.all_line);
        pcgameLine=getActivity().findViewById(R.id.pcgame_line);
        owLine=getActivity().findViewById(R.id.ow_line);
        cbpkLine=getActivity().findViewById(R.id.cbpk_line);


    }
}
